﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class TblDonViDAO:IData<TblDonVi> 
 	{
		public TblDonViDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref TblDonVi item, int t) {
			 return connec.ExecuteNonQuery("sp_TblDonVi", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + TblDonVi.c_MaDonVi, item.MaDonVi), new SqlParameter("@" + TblDonVi.c_TenDonVi, item.TenDonVi),
				 new SqlParameter("@" + TblDonVi.c_MaTuyenPhat, item.MaTuyenPhat), new SqlParameter("@" + TblDonVi.c_BillBatDau, item.BillBatDau),
				 new SqlParameter("@" + TblDonVi.c_BillKetThuc, item.BillKetThuc), new SqlParameter("@" + TblDonVi.c_DaXoa, item.DaXoa));
		}

		public override TblDonVi GetItem(IDataReader dr)
		{
			TblDonVi obj = new TblDonVi();
			obj.MaDonVi = Convert.ToInt32(dr[TblDonVi.c_MaDonVi]);
			obj.TenDonVi = Convert.ToString(dr[TblDonVi.c_TenDonVi]);
			obj.MaTuyenPhat = Convert.ToInt32(dr[TblDonVi.c_MaTuyenPhat]);
			obj.BillBatDau = Convert.ToInt32(dr[TblDonVi.c_BillBatDau]);
			obj.BillKetThuc = Convert.ToInt32(dr[TblDonVi.c_BillKetThuc]);
			obj.DaXoa = Convert.ToBoolean(dr[TblDonVi.c_DaXoa]);
            try {
                obj.TenTuyenPhat = Convert.ToString(dr[TblDonVi.c_TenTuyenPhat]);
            }
            catch { }
			return obj;
		}

		public override List<TblDonVi> GetAll()
		{
			return GetAll("sp_GetAllTblDonVi", CommandType.StoredProcedure);
		}

		public override TblDonVi GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.MaDonVi == Convert.ToInt32(id));
		}
		public override TblDonVi GetItemById(TblDonVi item){
			throw new NotImplementedException();
		}

	}

 }
